package com.jintian.smart.kernel.switching.db;

import com.alibaba.fastjson.JSON;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamContainedType;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.converters.enums.EnumConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.SchemaOutputResolver;
import jakarta.xml.bind.annotation.*;
import lombok.Data;

import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据报文采集规则
 */
@Data
@XStreamAlias("rule")
@XmlRootElement(name = "rule")
@XmlAccessorType(XmlAccessType.FIELD)
public class ExtractRule implements Serializable {

    private static final long serialVersionUID = 1L;

    // @XStreamAlias("name")
    // @XmlAttribute
    // private String name;

    @XStreamAlias("parameters")
    @XStreamContainedType
    @XmlElementWrapper(name = "parameters")
    @XmlElement(name = "parameter")
    private List<RuleParameter> parameters = new ArrayList<>();

    @XStreamAlias("table")
    @XStreamImplicit(itemFieldName = "table")
    @XStreamContainedType
    @XmlElement(name = "table")
    private List<ExtractTable> tables = new ArrayList<>();

    @XStreamAlias("fileAdapter")
    @XStreamImplicit(itemFieldName = "fileAdapter")
    @XStreamContainedType
    @XmlElement(name = "fileAdapter")
    private List<ExtractFileAdapter> fileAdapters = new ArrayList<>();

    public static void main(String[] args) throws JAXBException, IOException {
        JAXBContext jc = JAXBContext.newInstance(ExtractRule.class);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SchemaOutputResolver outputResolver = new SchemaOutputResolver() {
            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
                // TODO Auto-generated method stub
                StreamResult ret = new StreamResult(out);
                ret.setSystemId("test");
                return ret;
            }
        };
        jc.generateSchema(outputResolver);
        System.out.println(out.toString());

        JAXBContext context = JAXBContext.newInstance(ExtractRule.class);

        XStream xstream = new XStream(new DomDriver("utf-8"));
        // Converter converter = new EnumConverter();
        xstream.registerConverter(new EnumConverter());
        com.thoughtworks.xstream.annotations.Annotations.configureAliases(xstream, ExtractRule.class);
        try (InputStream in = new FileInputStream(
                "E:\\gitStorage\\platform\\base\\jintian-system\\jintian-system\\builder\\dist\\model\\kczygl\\etl\\绿色矿山评审-企业填报.xml")) {
            // ExtractRule rule = ExtractRule.class.cast(xstream.fromXML(in));
            ExtractRule rule = ExtractRule.class.cast(context.createUnmarshaller().unmarshal(in));
            System.out.println(JSON.toJSONString(rule));
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
}
